package puzzle.game.states;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import puzzle.core.LevelMap;
import puzzle.game.BallPuzzle;
import puzzle.game.MapList;
import puzzle.game.states.IngameState;
import puzzle.game.states.LoadingState;

public class UserIngameState extends IngameState {
  
  public static final int ID = 4;
  
  protected String changeStr = "Use Z and X for previous/next maps.";
  private int fheight=0;
  private int fwidth=0;
  private int fwidth2=0;
  private boolean started = false;
  
  public UserIngameState(BallPuzzle ctx) {
    super(ctx);
    this.titleStr = "BPuz User Maps";
  }
  
  public String getMapText(MapList.Entry e) {
    return e.toString();
  }
  
  public void endLevel() {
    if (maps.size()!=0) {
      currentMap++;
      if (currentMap > maps.size()-1) {
        currentMap = 0;
      }
    }
  }
  
  public void startState(GameContainer c) throws SlickException {
    super.startState(c);
    started = true;
  }
  
  public void renderState(GameContainer c, Graphics g) {
    super.renderState(c, g);
    
    if (!started)
      return;
    
    if (fheight==0) {
      fwidth = g.getFont().getWidth(escStr);
      fheight = g.getFont().getHeight(escStr);
    
      fwidth2 = g.getFont().getWidth(changeStr);
    }
    
    //right-aligns the text
    int x = helpx+fwidth - fwidth2;
    int y = helpy+fheight;
    
    g.setColor(Color.white);
    g.drawString(changeStr, x, y);
  }
  
  public MapList getStateMapList() {
    return getContext().getLoadingState().getUserMapList();
  }
  
  public int getID() {
    return ID;
  }
  
  public void keyPressed(int key, char c) {
    super.keyPressed(key, c);
    if (key == Input.KEY_Z) {
      currentMap--;
      if (currentMap<0)
        currentMap = maps.size()-1;
    }
    else if (key == Input.KEY_X) {
      endLevel();
    }
  }
}
